package jmine.tec.aom.domain;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Transient;

import jmine.tec.persist.annotation.DiscriminatorComment;

import org.hibernate.validator.NotNull;

import bancosys.tec.component.Documentation;
import bancosys.tec.persist.Persistable;
import bancosys.tec.utils.reflection.ReflectionUtils;

/**
 * Defines a {@link Persistable} based {@link Property}.
 * <p>
 * {@link AbstractPropertyValue} instances of this {@link Property} will hold the value of the
 * {@link bancosys.tec.persist.persister.annotation.NaturalKey} property of the represented {@link Persistable}.
 * <p>
 * Although very versatile this kind of {@link Property} does not allow foreign keys in the database. Relational integrity can only be
 * enforced using {@link bancosys.tec.persist.validator.Validator}.
 * 
 * @author piercio
 */
@Entity
@DiscriminatorValue(value = "2")
@DiscriminatorComment("ENTIDADE")
public class EntityProperty extends Property {

    private String entityType;

    /**
     * @return the fully qualified name of the entity class
     */
    @NotNull
    @Column(name = "FULLY_QUALIFIED_NAME")
    @Documentation("NOME DA CLASSE A SER UTILIZADA EM REFLECTION")
    public String getEntityType() {
        return this.entityType;
    }

    /**
     * @param entityType the fully qualified name of the entity class
     */
    public void setEntityType(String entityType) {
        this.entityType = entityType;
    }

    /**
     * @return the actual {@link Persistable} type to be used
     * @throws ClassNotFoundException if the given type cannot be found
     */
    @Transient
    @SuppressWarnings("unchecked")
    public Class<? extends Persistable> getEntityClass() throws ClassNotFoundException {
        return (Class<? extends Persistable>) ReflectionUtils.findClass(this.getEntityType());
    }

}
